/*******************************************************************************
 * Copyright 2012 Chao Zhang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package org.jbiolib.io;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

/**
 * @author Chao Zhang
 * @version 1.0 2012-11-06
 */

public class FileWrite {
	public static boolean removeFile(String MyFilePath) {
		boolean tag = true;
		try {
			File oldFile = new File(MyFilePath);
                        if(oldFile.exists()) {
                            oldFile.delete();
                        }
		} catch (Exception e) {
			tag = false;
			e.printStackTrace();
		}
		return tag;
	}
	
	public static boolean writeInput(String[] a, String MyFilePath) {
		boolean tag = true;
		String[] dataArray = a;
		try {
			FileWriter writer = new FileWriter(MyFilePath, true);
			BufferedWriter bufWriter = new BufferedWriter(writer);
			for(int i=0;i<dataArray.length;i++){
				bufWriter.write(dataArray[i]);
				bufWriter.newLine();			
			}
			bufWriter.close();
			writer.close();
		} catch (Exception e) {
			tag = false;
			e.printStackTrace();
		}
		return tag;
	}
	
	public static boolean writeInput(String a, String MyFilePath) {
		boolean tag = true;
		try {
			FileWriter writer = new FileWriter(MyFilePath, true);
			BufferedWriter bufWriter = new BufferedWriter(writer);
			bufWriter.write(a);
			bufWriter.newLine();			
			bufWriter.close();
			writer.close();
		} catch (Exception e) {
			tag = false;
			e.printStackTrace();
		}
		return tag;
	}
	
	public static boolean writeDoubleArray(double[][] value, String MyFilePath) {
		boolean tag = true;
		try {
			FileWriter writer = new FileWriter(MyFilePath);
			BufferedWriter bufWriter = new BufferedWriter(writer);
			for(int i=0;i<value.length;i++) {
				for(int j=0;j<value[0].length;j++) {
					bufWriter.write(value[i][j]+"\t");
				}
				bufWriter.newLine();
			}
			bufWriter.close();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tag;
	}
	
	public static boolean writeDoubleArray(double[] value, String MyFilePath) {
		boolean tag = true;
		try {
			FileWriter writer = new FileWriter(MyFilePath);
			BufferedWriter bufWriter = new BufferedWriter(writer);
			for(int i=0;i<value.length;i++) {
				bufWriter.write(value[i]+"\t");
			}
			bufWriter.close();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tag;
	}
	
	public static boolean writeMProb(double[][] probValue, String MyFilePath, int startIndex) {
		boolean tag = true;
		try {
			FileWriter writer = new FileWriter(MyFilePath);
			BufferedWriter bufWriter = new BufferedWriter(writer);
			for(int j=0;j<probValue[0].length;j++) {
				for(int i=startIndex;i<probValue.length;i++) {
					bufWriter.write(probValue[i][j]+"\t");
				}
				bufWriter.newLine();
			}
			bufWriter.close();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tag;
	}
	
	public static void writeIProb(double[] probValue, String MyFilePath, int startIndex) {
		try {
			FileWriter writer = new FileWriter(MyFilePath);
			BufferedWriter bufWriter = new BufferedWriter(writer);
			for(int i=startIndex;i<probValue.length-2;i++) {
				bufWriter.write(probValue[i]+"");
				bufWriter.newLine();
			}
			bufWriter.close();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

